package novel.mzx.com.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.view.View;

import com.bumptech.glide.Glide;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.Hashtable;
import java.util.List;

import cn.jzvd.JZVideoPlayerStandard;
import novel.mzx.com.R;
import novel.mzx.com.activity.PhotoVideoActivity;
import novel.mzx.com.activity.PhotoViewActivity;
import novel.mzx.com.bean.CircleBean;
import novel.mzx.com.bean.VideoBean;
import novel.mzx.com.http.Api;
import novel.mzx.com.views.RoundImageView;
import novel.mzx.com.views.XCRoundImageView;


/**
 * Created by Administrator on 2019/4/17 0017.
 */

public class VideoAdapter extends CommonAdapter<VideoBean.Data> {
    Context mContext;

    public VideoAdapter(Context context, int layoutId, List<VideoBean.Data> datas) {
        super(context, layoutId, datas);

        mContext = context;

    }
    JZVideoPlayerStandard videoPlayer;
    @Override
    protected void convert(ViewHolder holder, final VideoBean.Data s, int position) {


        holder.setText(R.id.tv_01,s.getVideoName());
        holder.setText(R.id.tv_02,"内容："+s.getVideoContent());



        RoundImageView one_image_iv0 = holder.getConvertView().findViewById(R.id.iv_bg);
        String jzCircleFile = s.getVideoImg();
        Bitmap videoThumbnail = createVideoThumbnail(Api.mainImgHost + jzCircleFile, MediaStore.Images.Thumbnails.MINI_KIND);
        one_image_iv0.setImageBitmap(videoThumbnail);

        holder.setOnClickListener(R.id.iv_bg, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PhotoVideoActivity.class);
                intent.putExtra("imageUrl",Api.mainImgHost+s.getVideoImg());
                mContext.startActivity(intent);
            }
        });
    }

    public static Bitmap createVideoThumbnail(String filePath, int kind)
    {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try
        {
            if (filePath.startsWith("http://")
                    || filePath.startsWith("https://")
                    || filePath.startsWith("widevine://"))
            {
                retriever.setDataSource(filePath, new Hashtable<String, String>());
            }
            else
            {
                retriever.setDataSource(filePath);
            }
            bitmap = retriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC); //retriever.getFrameAtTime(-1);
        }
        catch (IllegalArgumentException ex)
        {
            // Assume this is a corrupt video file
            ex.printStackTrace();
        }
        catch (RuntimeException ex)
        {
            // Assume this is a corrupt video file.
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                retriever.release();
            }
            catch (RuntimeException ex)
            {
                // Ignore failures while cleaning up.
                ex.printStackTrace();
            }
        }

        if (bitmap == null)
        {
            return null;
        }

        if (kind == MediaStore.Images.Thumbnails.MINI_KIND)
        {//压缩图片 开始处
            // Scale down the bitmap if it's too large.
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int max = Math.max(width, height);
            if (max > 512)
            {
                float scale = 512f / max;
                int w = Math.round(scale * width);
                int h = Math.round(scale * height);
                bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
            }//压缩图片 结束处
        }
        else if (kind == MediaStore.Images.Thumbnails.MICRO_KIND)
        {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap,
                    96,
                    96,
                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
    }

}
